#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int getMoneyAmount(int n) {
        vector<vector<int>> memo(n + 1, vector<int>(n + 1, -1));
        return dfs(1, n, memo);
    }
    int dfs(int left, int right, vector<vector<int>>& memo) {

        if (left >= right)
            return 0;
        if (memo[left][right] != -1)
            return memo[left][right];
        int ret = INT_MAX;
        for (int head = left; head <= right; head++) {
            int x = dfs(left, head - 1, memo);
            int y = dfs(head + 1, right, memo);
            ret = min(head + max(x, y), ret);
        }
        memo[left][right] = ret;
        return ret;
    }
};